package cn.com.dashihui.web.service;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;

import cn.com.dashihui.kit.ExcelExportUtil;
import cn.com.dashihui.kit.ExcelExportUtil.Pair;
import cn.com.dashihui.web.dao.Pref;

public class PrefService {

	public List<Pref> findTicketList() {
		// TODO Auto-generated method stub
		return Pref.me().find("select * from t_pref");
	}

	/**
	 * 分页查找
	 */
	public Page<Record> findByPage(int pageNum, int pageSize, String id, int type) {
		StringBuffer sBuffer = new StringBuffer(" FROM t_pref T WHERE 1=1 and status=1 ");
		List<Object> params = new ArrayList<Object>();
		if (!"null".equals(id)) {
			sBuffer.append(" AND T.id=?");
			params.add(id);
		}
		switch (type) {
		case 1:
			sBuffer.append(" AND T.type=1 ");
			break;
		case 2:
			sBuffer.append(" AND T.type=2");
			break;
		case 3:
			sBuffer.append(" AND T.type=3");
			break;
		case 4:
			sBuffer.append(" AND T.type=4");
			break;
		case 5:
			sBuffer.append(" AND T.type=5");
			break;
		}

		sBuffer.append(" ORDER BY T.id DESC");
		return Db.paginate(pageNum, pageSize, "SELECT T.*", sBuffer.toString(), params.toArray());
	}

	public Record detail(int id) {
		return Db.findById("t_pref", id);
	}

	public List<Record> getCategoryById(int id) {
		return Db.find("select * from t_dict_category where categoryId =?", id);
	}

	public Record getGoodById(int id) {
		return Db.findById("t_bus_goods", id);
	}

	public Pref findPref(int id) {
		return Pref.me().findById(id);
	}

	public boolean editPref(Pref object) {
		return object.update();
	}

	/**
	 * 导出数据
	 * 
	 * @param response
	 * @param request
	 * @param listz
	 * @param titles2
	 * @param member
	 */
	public void export(HttpServletResponse response, HttpServletRequest request, List<Pair> titles,
			List<Record> listz) {
		ExcelExportUtil.exportByRecord(response, request, "优惠券列表", titles, listz);
	}

	public List<Record> findExcelByPage(int type, String id) {
		StringBuffer sBuffer = new StringBuffer(
				"SELECT T.id,T.title,T.type,T.count,T.limitcount,T.uselimitmoney,T.values,T.discount,T.createtime,T.validstarttime, T.validendtime, T.validday FROM t_pref T  WHERE 1=1 and status=1 ");
		List<Object> params = new ArrayList<Object>();

		if (!StrKit.isBlank(id) && !"0".equals(id)) {
			sBuffer.append(" AND T.id=?");
			params.add(id);
		}
		switch (type) {
		case 1:
			sBuffer.append(" AND T.type=1 ");
			break;
		case 2:
			sBuffer.append(" AND T.type=2");
			break;
		case 3:
			sBuffer.append(" AND T.type=3");
			break;
		case 4:
			sBuffer.append(" AND T.type=4");
			break;
		case 5:
			sBuffer.append(" AND T.type=5");
			break;
		}
		sBuffer.append(" ORDER BY T.id DESC");
		return Db.find(sBuffer.toString(), params.toArray());

	}

	public boolean addPref(Pref newObject) {
		return newObject.save();
	}

}
